Rem  Copyright (c) 1999, 2000 by Marcelo F. Ochoa.  All Rights Reserved.
Rem
Rem   NAME
Rem     xtp.bdy - DB Prism Xml Toolkit Procedures (xtp)
Rem   PURPOSE
Rem     Install the PL/SQL needed to run the XML Application.
Rem   NOTES
Rem
Rem   HISTORY
Rem     mochoa@ieee.org   04/02/02 -
Rem       add comment procedure
Rem     mochoa@ieee.org   07/02/02 -
Rem   Tags for Cocoon2 architecture and DBPrismGenerator
Rem     mochoa@ieee.org   11/17/00 -
Rem		replace xtp.tag(cname,cvalue,bclose)
Rem		by xtp.tag(cname,cvalue,cattributes)
Rem		correct syntax of xtp.send according to a new format of date
Rem		in HeaderProcessor class
Rem     mochoa@ieee.org   1/7/00 -  Creation version 1.0.0
Rem

CREATE OR REPLACE PACKAGE body xtp IS
-- version 2.0.0
----------------------------------------------
-- REQUIRES HTP TOOLKITS of 8.1.7+ installed -
-- by default it is installed on sys schema --
----------------------------------------------
-- obsoletes procedures:
--   xtp.prolog(cversion,cstylesheet,cmedia,cattrbutes) replaced by xtp.prolog(cversion)
--   xtp.stylesheet(...)
--   xtp.process(...)
--   xtp.not_found
-- new procedures:
--   xtp.comment(ccomment)
--   xtp.header(cContentType,bCloseHeader)
--   xtp.setEncoding(cEncoding)
--   xtp.setContentType(cContentType)
--   xtp.setHeader(cName,cValue)
--   xtp.closeHeader
-- new attributes
--   contentType
----------------------------------------------
-- version 1.0.1
--   replaced xtp.tag(cname,cvalue,bclose)
--   by xtp.tag(cname,cvalue,cattributes)
--   correct syntax of xtp.send according to a new format of date in HeaderProcessor class
--
----------------------------------------------
-- version 1.0.0
--   initial
   notInitialize boolean := true;
   isHeaderClosed   boolean := false;

   /* The broken line below is intentional */
   NL_CHAR  constant  char(1) := '
';

procedure initialize is
begin
    xtp.header('text/xml');
end initialize;

procedure prolog(cversion in VARCHAR2 default '1.0')  is
begin
   xtp.p(xtf.prolog(cversion));
end prolog;

procedure comment(ccomment in VARCHAR2)  is
begin
   xtp.p(xtf.comment(ccomment));
end comment;

procedure epilog is
begin
   -- line optional, because print toolkit version as xml comment
   xtp.p(xtf.epilog);
   -- set parameters for next invocation, specially when use transactions
   -- because global parameters are not reseted
   notInitialize := true;
   isHeaderClosed := false;
end epilog;

procedure tag(cname in VARCHAR2,
		cvalue in VARCHAR2,
		cattributes in VARCHAR2 default NULL) is
begin
   xtp.p(xtf.tag(cname,cvalue,cattributes));
end tag;

procedure tagOpen(cname in VARCHAR2, cattributes in VARCHAR2 default NULL) is
begin
   xtp.p(xtf.tagOpen(cname,cattributes));
end tagOpen;

procedure tagClose(cname in VARCHAR2) is
begin
   xtp.p(xtf.tagClose(cname));
end tagClose;


/* SPECIAL PROCEDURES */
procedure print (cbuf in varchar2 DEFAULT NULL) is
begin
   if notInitialize then
     xtp.initialize;
   end if;
   htp.print(cbuf);
end;

/* Print, but without the newline */
procedure prn (cbuf in varchar2 DEFAULT NULL) is
begin
   if notInitialize then
     xtp.initialize;
   end if;
   htp.prn(cbuf);
end;

procedure print (dbuf in date) is
begin print(to_char(dbuf)); end;

procedure print (nbuf in number) is
begin print(to_char(nbuf)); end;

procedure prn (dbuf in date) is
begin prn(to_char(dbuf)); end;

procedure prn (nbuf in number) is
begin prn(to_char(nbuf)); end;

procedure p (cbuf in varchar2 DEFAULT NULL) is
begin print(cbuf); end;

procedure p (dbuf in date) is
begin print(to_char(dbuf)); end;

procedure p (nbuf in number) is
begin print(to_char(nbuf)); end;

procedure prints(ctext in varchar2) is
begin p(htf.escape_sc(ctext)); end;

procedure ps(ctext in varchar2) is
begin p(htf.escape_sc(ctext)); end;

procedure escape_sc(ctext in varchar2) is
begin p(htf.escape_sc(ctext)); end;
/* END SPECIAL PROCEDURES */

function IFNOTNULL(val1 in varchar2,
                      val2 in varchar2) return varchar2 is
begin
      if (val1 is not null)
      then
         return val2;
      else
         return NULL;
      end if;
end;


procedure send(name    in varchar2,
               value   in varchar2,
               expires in date     DEFAULT NULL,
               path    in varchar2 DEFAULT NULL,
               domain  in varchar2 DEFAULT NULL,
               secure  in varchar2 DEFAULT NULL) is
begin
   owa_cookie.send(name,value,expires,path,domain,secure);
end;

function get(name in varchar2) return owa_cookie.cookie is
begin
      return owa_cookie.get(name);
end;

procedure remove(name in varchar2,
                 val  in varchar2,
                 path in varchar2 DEFAULT NULL) is
begin
      owa_cookie.remove(name,val,path);
end;

procedure get_all(names    out owa_cookie.vc_arr,
                  vals     out owa_cookie.vc_arr,
                  num_vals out integer) is
begin
      owa_cookie.get_all(names,vals,num_vals);
end;

procedure header(cContentType in varchar2, bCloseHeader boolean default true) is
begin
  notInitialize := false;
  if xtf.encoding = 'notInitialized' then
    xtf.encoding := owa_util.get_cgi_env('REQUEST_IANA_CHARSET');
    if xtf.encoding is null then
      xtf.encoding := xtf.defaultEncoding;
    end if;
  end if;
  xtp.contentType := cContentType;
  if not isHeaderClosed then
    owa_util.mime_header(cContentType,bCloseHeader,xtf.encoding);
    isHeaderClosed := bCloseHeader;
  end if;
end header;

procedure closeHeader is
begin
  if not isHeaderClosed then
    owa_util.http_header_close;
  end if;
  isHeaderClosed := true;
end closeHeader;

procedure setHeader(cName in varchar2, cValue in varchar2) is
begin
     if not isHeaderClosed then
        xtp.p(cName||': '||cValue);
     end if;
end;

procedure setEncoding(cEncoding in varchar2) is
begin
   xtf.encoding := cEncoding;
end setEncoding;

procedure setContentType(cContentType in varchar2) is
begin
   xtp.contentType := cContentType;
end setContentType;

procedure redirect_url(curl in varchar2) is
begin
    owa_util.redirect_url(curl);
    notInitialize := false;
    isHeaderClosed := true;
    xtp.p('<dummy/>');
    xtp.epilog();
end;

procedure not_authorized(realm in varchar2) is
begin
    xtp.header('text/xml',false);
    xtp.setHeader('WWW-authenticate',realm);
    xtp.closeHeader();
    xtp.p('<dummy/>');
    xtp.epilog();
end;

begin
   notInitialize := true;
   isHeaderClosed := false;
END xtp;
/
